package com.example.ivos.dict.service.impl;

import com.example.ivos.base.exception.ServiceException;
import com.example.ivos.base.response.StatusCode;
import com.example.ivos.dict.mapper.DictMapper;
import com.example.ivos.dict.pojo.dto.DictParam;
import com.example.ivos.dict.pojo.dto.DictQuery;
import com.example.ivos.dict.pojo.entity.Dict;
import com.example.ivos.dict.pojo.vo.DictVO;
import com.example.ivos.dict.service.DictService;
import com.example.ivos.dictoption.mapper.DictOptionMapper;
import com.example.ivos.dictoption.pojo.entity.DictOption;
import com.example.ivos.dictoption.pojo.vo.DictOptionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private DictOptionMapper dictOptionMapper;

    @Override
    public List<DictVO> select(DictQuery dictQuery) {
        log.debug("查询字典列表业务dictQuery:{}", dictQuery);
        Dict dict=new Dict();
        BeanUtils.copyProperties(dictQuery, dict);
        List<DictVO> dictVOList = dictMapper.select(dict);
        return dictVOList;
    }

    @Override
    public void save(DictParam dictParam) {
        log.debug("字典管理业务dicParam:{}", dictParam);
        Dict dict=new Dict();
        BeanUtils.copyProperties(dictParam,dict);
        if(dict.getId()==null){
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }
        else{
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }
    }

    @Override
    public void del(Long id) {
        log.debug("删除字典业务id:{}", id);
        dictOptionMapper.deleteByDictId(id);
        dictMapper.del(id);
    }
}
